package com.agriculture.competition.utils;

import com.agriculture.competition.domain.vo.JaDateTableDownLoadVo;
import com.alibaba.excel.EasyExcel;
import java.io.IOException;
import java.net.URLEncoder;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Component;

/**
 * @author zss
 */
@Component
public class ExcelDownLoadUtil {

    public static void downLoad(HttpServletResponse response, JaDateTableDownLoadVo jaDateTableDownLoadVo)
            throws IOException {
        // List<Map<String, String>> list = new ArrayList<>();
        // Map<String, String> map = new HashMap<>();
        // map.put("name", "张三");
        // map.put("age", "19");
        // list.add(map);
        // list.add(map);
        // list.add(map);
        // List<String> header = new ArrayList<>();
        // header.add("value1");
        // header.add("value2");
        // header.add("value3");
        String fileName = URLEncoder.encode(jaDateTableDownLoadVo.getFileName(), "UTF-8").replaceAll("\\+", "%20");
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        // 防止中文乱码
        response.setHeader("Content-Disposition", "attachment;filename=" + fileName + ".xlsx");
        response.setHeader("mime", "application/vnd.ms-excel");
        EasyExcel.write(response.getOutputStream())
                .head(jaDateTableDownLoadVo.getHeader())
                .sheet(fileName)
                .doWrite(jaDateTableDownLoadVo.getList());
    }
}
